import API from '../../api/api.js'
const app = getApp()
Page({

  /**
   * 页面的初始数据
   */
  data: {
    PageCur: 'home',
    screenHeight: 0,
    artileList: [],
    isLogin: false
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    this.data.screenHeight = app.globalData.screenHeight
    this.setData({
      screenHeight: this.data.screenHeight
    })
    wx.setStorageSync('isLogin', false)
    wx.getSetting({
      success: res => {
        if (res.authSetting['scope.userInfo']) {
          wx.showLoading({
            title: '正在加载...',
            mask: true
          })
          API.getOpenID().then(res => {
            API.getUserDataByOpenId(res).then(res => {
              if (res.length != 0) {
                this.data.isLogin = true
                wx.setStorageSync('isLogin', true)
                wx.setStorageSync('userInfo', res[0])
                this.setData({
                  isLogin: true
                })
              }
              
              wx.hideLoading()
            })
          })
        }
      }
    })

    API.getRandomArticle("wenzhangbiao", 5).then(res => {
      this.setData({
        dataList: res
      })
    })
  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
    const updateManager = wx.getUpdateManager()
    updateManager.onCheckForUpdate(function (res) {})
    updateManager.onUpdateReady(function () {
      wx.showModal({
        title: '更新提示',
        content: '新版本已经准备好，是否重启应用？',
        success: function (res) {
          if (res.confirm) {
            updateManager.applyUpdate()
          }
        }
      })
    })
    updateManager.onUpdateFailed(function () {
      // 新版本下载失败
    })
  },
  navChange(e) {
    if(!this.checkLogin()){
      if(e.currentTarget.dataset.cur == "my"|| e.currentTarget.dataset.cur == "home"){
       
      }else{
        wx.showToast({
          title: '您还没有登录，请登录查看内容。',
          icon:"none"
        })
        return
      }
    }
    this.setData({
      PageCur: e.currentTarget.dataset.cur
    })
  },

  checkLogin:function(e){
    let isLogin = wx.getStorageSync('isLogin')
    if(isLogin){
      return true
    }else{
      return false
    }
  },

  onFaBu: function (e) {
    if(!this.checkLogin()){
      wx.showToast({
        title: '您还没有登录，请登录查看内容。',
        icon:"none"
      })
      return
    }
    wx.navigateTo({
      url: "/pages/fabiaowenzhang/fabiaowenzhang"
    })
  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {
    let shareimages = app.globalData.shareimages
    let index = Math.floor(Math.random() * 5)
    return {
      title: '一个真正匿名的心情小程序',
      imageUrl: shareimages[index],
      path: '/pages/index/index'
    }
  },
  onShareTimeline: function () {
let shareimages = app.globalData.shareimages
    let index = Math.floor(Math.random() * 5)
    return {
      title: '一个真正匿名的心情小程序',
      imageUrl: shareimages[index]
    }
  }
})